﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SharpDiary.Core.Common;
using SharpDiary.Core.Attributes;
using SharpDiary.Core.ORMapping;
using SharpDiary.Core.Utility;

namespace SharpDiary.Emotion
{
    [GuiModule("35b41107-af01-4318-ae0e-8dbbe0cd5711", "EmotionEditor", "Ray Shaw(demogodyou@gmail.com)")]
    public partial class frmEmotionEditor : GuiModuleUIBase
    {
        #region Fields & Properties

        private EmotionInfo _emotion;

        public EmotionInfo Emotion
        {
            get { return _emotion; }
            set
            {
                _emotion = value;
                DisplayEmotionInfo();
            }
        }

        #endregion

        #region Constructors

        public frmEmotionEditor()
        {
            InitializeComponent();
        }

        #endregion

        #region Methods

        public override string GetSettingsKeyName()
        {
            return this.Name;
        }

        public override void SetParameter(object parameter, object sender)
        {
            base.SetParameter(parameter, sender);

            Emotion = parameter as EmotionInfo;
        }

        /// <summary>
        /// 显示心情图示信息。
        /// </summary>
        private void DisplayEmotionInfo()
        {
            if (_emotion == null)
            {
                txtName.Clear();
                txtDescription.Clear();
                pictureBoxEmotion.Image = null;

                return;
            }

            txtName.Text = _emotion.Name;
            txtDescription.Text = _emotion.Description;
            pictureBoxEmotion.Image = Utils.BytesToImage(_emotion.Picture);
        }

        /// <summary>
        /// 选择图示文件。
        /// </summary>
        private void ChooseImage()
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Multiselect = false;
                ofd.Filter = "图示文件(*.bmp, *.jpg, *.jpeg, *.ico, *.png)|*.bmp;*.jpg;*.jpeg;*.ico;*.png";

                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    Image img = Image.FromFile(ofd.FileName);
                    if (img.Size != new Size(32, 32))
                    {
                        pictureBoxEmotion.Image = img.GetThumbnailImage(32, 32, null, IntPtr.Zero);
                    }
                    else
                    {
                        pictureBoxEmotion.Image = img;
                    }
                }
            }
        }

        /// <summary>
        /// 输入数据有效性检查。
        /// </summary>
        /// <returns></returns>
        private bool CheckIfInputOk()
        {
            if (pictureBoxEmotion.Image == null)
            {
                popupMessageControl.Show("请选择图示。");
                return false;
            }

            if (txtName.Text.Trim().Length == 0)
            {
                popupMessageControl.Show("请输入图示名称。");
                txtName.Focus();
                txtName.SelectAll();

                return false;
            }


            return true;
        }

        /// <summary>
        /// 保存。
        /// </summary>
        private void Save()
        {
            if (!CheckIfInputOk()) { return; }

            if (_emotion == null || string.IsNullOrEmpty(_emotion.Name))
            {
                _emotion = new EmotionInfo();
                _emotion.Name = txtName.Text;
                _emotion.Description = txtDescription.Text;
                _emotion.ValidStatus = 1;
                _emotion.Picture = Utils.ImageToBytes(pictureBoxEmotion.Image);

                if (!EmotionInfo.Create<EmotionInfo>(_emotion))
                {
                    popupMessageControl.Show("保存失败。");
                }
                else
                {
                    popupMessageControl.Show("保存成功，您可以继续添加心情图示或取消。");
                    Emotion = new EmotionInfo();
                }
            }
            else
            {
                _emotion.Name = txtName.Text;
                _emotion.Description = txtDescription.Text;
                _emotion.Picture = Utils.ImageToBytes(pictureBoxEmotion.Image);
                _emotion.ValidStatus = 1;

                if (!EmotionInfo.Update<EmotionInfo>(_emotion))
                {
                    popupMessageControl.Show("更新失败。");
                }
                else
                {
                    popupMessageControl.Show("更新成功。");
                    this.DialogResult = DialogResult.OK;
                    Close();
                }
            }
        }

        private void btnChooseImage_Click(object sender, EventArgs e)
        {
            ChooseImage();

            txtName.Focus();
            txtName.SelectAll();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void pictureBoxEmotion_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                btnChooseImage.PerformClick();
            }
        }

        #endregion
    }
}
